<?php
/**
 * Validierungsklasse zur Parametervalidierung
 *
 * @author		Joachim Ruf
 * @copyright	Copyright (c) 2003-2013 Loresoft Software (http://www.loresoft.de)
 * @license		http://yapary.loresoft.de/lizenzbedingungen     New BSD License
 *
 * */


/**
 * $val muss einer Zahlenfolge entsprechen
 * */
Class Yapary_Parameter_CheckNumeric Extends Yapary_Exception_Error Implements Yapary_Parameter_ICheck
{
	private $min, $max;
	function __construct ( $_min = NULL, $_max = NULL )
	{
		$this->min = $_min;
		$this->max = $_max;
	}


	public function typedef ( $val )
	{
		if (strlen($val) <= 10) {
			return (int) $val;
		}
		return (string) $val;
	}


	public function getPattern ()
	{
		return '^[0-9]{1,}$';
	}


	public function isValid ( $val )
	{
		if (!isset($val)) {
			$this->addError(Yapary_Exception_Error::E_UNDEFINED);
			return false;
		}
		if ($this->min != NULL && $val < $this->min) {
			$this->addError(Yapary_Exception_Error::E_OUT_OF_SCOPE);
			return false;
		}
		if ($this->max != NULL && $val > $this->max) {
			$this->addError(Yapary_Exception_Error::E_OUT_OF_SCOPE);
			return false;
		}
		if (!preg_match('/'.$this->getPattern().'/', $val)) {
			$this->addError(Yapary_Exception_Error::E_WRONG_FORMAT);
			return false;
		}

		return true;
	}
}
